﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace WEBCamera.Web
{
    /// <summary>
    /// Capture for full element screenshoot and body title like defult page on Box Suit Office 365 wave 15 site.
    /// </summary>
    class CombinScreenShot
    {
        public static int absoX { get; set; }

        public static int absoY { get; set; }

        /// <summary>
        /// Combine the the element screenshoot into body screenshoot.
        /// </summary>
        /// <param name="savePath">will save the combine screenshoot</param>
        /// <param name="fileBody">body screenshoot file path</param>
        /// <param name="fileEle">element screenshoot file path</param>
        /// <param name="eleAbsoWidth">element absoluted page left location</param>
        /// <param name="eleAbsoHeight">element absoluted page top location</param>
        /// <returns></returns>
        public string shootCombine(string savePath, string fileBody, string fileEle, int eleAbsoWidth, int eleAbsoHeight)
        {
            int mapWidth;
            int mapHeight;
            Image imBody = Image.FromFile(fileBody);
            Image imEle = Image.FromFile(fileEle);

            if (imEle.Width + eleAbsoWidth > imBody.Width)
            {
                mapWidth = imEle.Width + eleAbsoWidth;
            }
            else
            {
                mapWidth = imBody.Width;
            }
            if (imEle.Height + eleAbsoHeight > imBody.Height)
            {
                mapHeight = imEle.Height + eleAbsoHeight;
            }
            else
            {
                mapHeight = imBody.Height;
            }

            Bitmap mapFullScreen = new Bitmap(mapWidth, mapHeight, PixelFormat.Format16bppRgb555);

            Graphics graphics = Graphics.FromImage(mapFullScreen);
            graphics.DrawImage(imBody, 0, 0);
            graphics.DrawImage(imEle, eleAbsoWidth, eleAbsoHeight);

            imBody.Dispose();
            imEle.Dispose();
            mapFullScreen.Save(savePath);
            deleteFile(new string[] { fileBody, fileEle });
            return savePath;
        }

        /// <summary>
        /// delete temp files.
        /// </summary>
        /// <param name="arrFiles">all temp file paths</param>
        private void deleteFile(params string[] arrFiles)
        {
            if (arrFiles.Length < 0)
            {
                return;
            }
            foreach (string file in arrFiles)
            {
                if (File.Exists(file))
                {
                    int i = 0;
                    File.Delete(file);
                    while (i < 10)
                    {
                        if (File.Exists(file))
                        {
                            System.Threading.Thread.Sleep(500);
                            i++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
}
